//
//  YXTopButtonView.m
//  MockProject
//
//  Created by FanYu on 23/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXTopButtonView.h"

@interface YXTopButtonView ()

@property (nonatomic, strong) UIButton *downloadingButton;
@property (nonatomic, strong) UIButton *downloadedButton;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *hairlineView;

@end

@implementation YXTopButtonView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


#pragma mark - Setup UI
- (void)setupUI {
    // self
    self.backgroundColor = [UIColor whiteColor];
    
    // downloading button
    self.downloadingButton = [[UIButton alloc] init];
    self.downloadingButton.tag = 0;
    [self.downloadingButton setImage:[UIImage imageNamed:@"下载中图标正常态"] forState:UIControlStateNormal];
    [self.downloadingButton setImage:[UIImage imageNamed:@"下载中图标当前选择态"] forState:UIControlStateSelected];
    [self.downloadingButton setImage:[UIImage imageNamed:@"下载中图标当前选择态"] forState:UIControlStateHighlighted];
    [self.downloadingButton setTitle:@"下载中..." forState:UIControlStateNormal];
    [self.downloadingButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateSelected];
    [self.downloadingButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateHighlighted];
    [self.downloadingButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [self.downloadingButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.downloadingButton sizeToFit];
    [self addSubview:self.downloadingButton];

    [self.downloadingButton setSelected:YES];
    
    [self.downloadingButton addTarget:self action:@selector(downloadingButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.downloadingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.mas_width).multipliedBy(0.5);
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    
    // downloaded button
    self.downloadedButton = [[UIButton alloc] init];
    self.downloadedButton.tag = 1;
    [self.downloadedButton setImage:[UIImage imageNamed:@"下载完成图表正常态"] forState:UIControlStateNormal];
    [self.downloadedButton setImage:[UIImage imageNamed:@"下载完成图表当前选择态"] forState:UIControlStateSelected];
    [self.downloadedButton setImage:[UIImage imageNamed:@"下载完成图表当前选择态"] forState:UIControlStateHighlighted];
    [self.downloadedButton setTitle:@"下载完成" forState:UIControlStateNormal];
    [self.downloadedButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateSelected];
    [self.downloadedButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateHighlighted];
    [self.downloadedButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [self.downloadedButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.downloadedButton sizeToFit];
    [self addSubview:self.downloadedButton];
    
    [self.downloadedButton addTarget:self action:@selector(downloadedButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.downloadedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.mas_width).multipliedBy(0.5);
        make.left.equalTo(self.downloadingButton.mas_right);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    
    // line view
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor colorWithHexString:@"e2e2e4"];
    [self addSubview:self.lineView];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(20);
    }];
    
    // hairline view 
    self.hairlineView = [[UIView alloc] init];
    self.hairlineView.backgroundColor = [UIColor blackColor];
    self.hairlineView.alpha = 0.2;
    [self addSubview:self.hairlineView];
    
    [self.hairlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.width.equalTo(self.mas_width);
        make.bottom.equalTo(self.mas_bottom);
    }];
}


#pragma mark - Actions 
- (void)downloadingButtonTapped:(UIButton *)sender {
    [self.delegate topButtonTapped:sender];
    self.downloadedButton.selected = NO;
    sender.selected = YES;
}

- (void)downloadedButtonTapped:(UIButton *)sender {
    [self.delegate topButtonTapped:sender];
    self.downloadingButton.selected = NO;
    sender.selected = YES;
}


#pragma mark - Heler 
- (void)setSelectedButton:(NSInteger) tag {
    if (tag == 0) {
        [self.downloadingButton setSelected:YES];
        [self.downloadedButton setSelected:NO];
    } else {
        [self.downloadingButton setSelected:NO];
        [self.downloadedButton setSelected:YES];
    }
}

@end
